package com.hlwxy.ssm.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Tools {
    /**
     * 检测字符串是否不为空(null,"","null")
     * @param s
     * @return 不为空则返回true，否则返回false
     */
    public static boolean notEmpty(String s)
    {
        return s != null && !"".equals(s) && !"null".equals(s);
    }

    /**
     * 检测字符串是否为空(null,"","null")
     * @param s
     * @return 为空则返回true，不否则返回false
     */
    public static boolean isEmpty(String s)
    {
        return s == null || "".equals(s) || "null".equals(s);
    }

    /**
     * 字符串转换为字符串数组
     * @param str 字符串
     * @param splitRegex 分隔符
     * @return
     */
    public static String[] str2StrArray(String str, String splitRegex)
    {
        if (isEmpty(str))
        {
            return null;
        }
        return str.split(splitRegex);
    }

    /**
     * 用默认的分隔符(,)将字符串转换为字符串数组
     * @param str   字符串
     * @return
     */
    public static String[] str2StrArray(String str)
    {
        return str2StrArray(str, ",\\s*");
    }

    /**
     * 按照yyyy-MM-dd HH:mm:ss的格式，日期转字符串
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String date2Str(Date date)
    {
        return date2Str(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 按照yyyy-MM-dd HH:mm:ss的格式，字符串转日期
     * @param date
     * @return
     */
    public static Date str2Date(String date)
    {
        if (notEmpty(date))
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try
            {
                return sdf.parse(date);
            }
            catch (ParseException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return new Date();
        }
        else
        {
            return null;
        }
    }

    /**
     * 按照参数format的格式，日期转字符串
     * @param date
     * @param format
     * @return
     */
    public static String date2Str(Date date, String format)
    {
        if (date != null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
        else
        {
            return "";
        }
    }

    /**
     * 字符串为null的返回""
     * @param date
     * @param format
     * @return
     */
    public static String nullToEmpty(String str)
    {
        if(isEmpty(str)){
            return "";
        }
        return str;
    }
    public static String getTime(String dateTime){
        String returnTime=dateTime.substring(0,10);
        String nowDate=new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(new Date());
        long leftTime=(Tools.str2Date(dateTime).getTime())-Tools.str2Date(nowDate).getTime();
        if(Math.floor(leftTime/1000/60/60)<=24){
            int h=Integer.parseInt(dateTime.substring(11,13));
            if(h==0){
                returnTime="今天23:59";
            }
            else{
                returnTime="今天"+dateTime.substring(11,16);
            }
        }
        return returnTime;
    }

    /*时间比大小  -1为过期*/
    public static int timeCompare(String t1){
        int result = 0 ;
        if(null != t1){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c1=Calendar.getInstance();
            Calendar c2=Calendar.getInstance();
            try {
                c1.setTime(formatter.parse(t1));
                c2.setTime(formatter.parse(getCurrentTime()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            result=c1.compareTo(c2);
        }
        return result;
    }

    public static String getCurrentTime(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
}
